package com.example.drop

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import androidx.activity.viewModels

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.drop.databinding.ActivityPointAddBinding
import com.example.drop.entity.PointAddParam
import com.example.drop.vm.PointViewModel
import com.example.image.glide.GlideImgLoader
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/drop/PointAddActivity")
class PointAddActivity : BaseMvvmActivity<ActivityPointAddBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_point_add
    val pointViewModel:PointViewModel by viewModels ()
    override fun initView() {
        super.initView()
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        //类型选择器
        mDataBinding.ivProject.setOnClickListener {
            val list = mutableListOf("项目1","项目2","项目3")
            val optionPiker= OptionPicker(this)
            optionPiker.setData(list)
            optionPiker.setTitle("选择所属项目")
            optionPiker.setOnOptionPickedListener { position, item ->
                mDataBinding.edProject.text= Editable.Factory.getInstance().newEditable(list[position])
            }
            optionPiker.show()
        }
        mDataBinding.ivType.setOnClickListener {
            val list = mutableListOf("安防巡检","保洁巡检","日常巡检","家居巡检")
            val optionPiker= OptionPicker(this)
            optionPiker.setData(list)
            optionPiker.setTitle("选择巡检类型")
            optionPiker.setOnOptionPickedListener { position, item ->
                mDataBinding.edType.text= Editable.Factory.getInstance().newEditable(list[position])
            }
            optionPiker.show()
        }
        mDataBinding.ivTime.setOnClickListener {
            val list = mutableListOf("优秀","良好","差")
            val optionPiker= OptionPicker(this)
            optionPiker.setData(list)
            optionPiker.setTitle("选择巡检标准")
            optionPiker.setOnOptionPickedListener { position, item ->
                mDataBinding.edTime.text= Editable.Factory.getInstance().newEditable(list[position])
            }
            optionPiker.show()
        }
        mDataBinding.ivIcon.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        mDataBinding.btnCommit.setOnClickListener {
            var pointAddParam=PointAddParam(
             id=0,
             patrolStandard= "1,2,3",
             patrolType=mDataBinding.edType.text.toString(),
             pointImages="",
             pointLocation= SPUtils.getInstance().getString("gaode").toString(),
             pointName=mDataBinding.edPointName.text.toString(),
             projectId=mDataBinding.edProject.text.toString()
            )
            pointViewModel.postPoint(pointAddParam)
        }
        pointViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
        pointViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==200&&resultCode== RESULT_OK){
            val bean=data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideImgLoader.loadImage(this,bean.path,mDataBinding.ivIcon)
        }
    }
}